package com.iplant.aps.service.po;

import com.iplant.aps.service.utils.Configuration;
import com.iplant.aps.service.utils.StringUtils;

public class Pagination {
	private Pagination() {
		Sort = "ID"; // 默认按Id排序
		SortType = " desc ";
		PageIndex = 1;
		PageSize = 10;
	}

	private Pagination(int wPageSize) {
		Sort = "ID"; // 默认按Id排序
		SortType = " desc ";
		PageIndex = 1;
		if (wPageSize <= 0)
			wPageSize = 10;
		PageSize = wPageSize;
	}

	private static int MaxPageSize = 100;

	static {

		MaxPageSize = StringUtils.parseInt(Configuration.readConfigString("page.size.max", "config/config"));

		if (MaxPageSize < 100)
			MaxPageSize = 500;
	}

	public static Pagination Create(int wPageIndex, int wPageSize, String... args) {
		if (wPageSize <= 0)
			wPageSize = MaxPageSize;
		Pagination wResult = new Pagination();

		wResult.PageIndex = wPageIndex;
		wResult.PageSize = wPageSize;
		if (args == null)
			return wResult;
		if (args.length > 0 && StringUtils.isNotEmpty(args[0]))
			wResult.Sort = args[0];
		if (args.length > 1 && StringUtils.isNotEmpty(args[1]))
			wResult.SortType = args[1];
		return wResult;
	}

	private static Pagination mDefault = new Pagination();

	public static Pagination getDefault() {

		return mDefault;

	}

	private static Pagination mMaxSize = new Pagination(Integer.MAX_VALUE);

	public static Pagination getMaxSize() {
		return mMaxSize;
	}

	public static Pagination getNewMaxSize() {
		return new Pagination(Integer.MAX_VALUE);
	}

	public static Pagination getNewDefault() {
		return new Pagination();
	}

	/// <summary>
	/// 每页行数
	/// </summary>
	public int PageSize = 0;
	/// <summary>
	/// 当前页
	/// </summary>
	public int PageIndex = 0;
	/// <summary>
	/// 排序列
	/// </summary>
	public String Sort = "";
	/// <summary>
	/// 排序类型
	/// </summary>
	public String SortType = "";
	/// <summary>
	/// 总记录数
	/// </summary>
	public int TotalCount = 0;

	@SuppressWarnings("unused")
	private int TotalPage = 0;

	/// <summary>
	/// 总页数
	/// </summary>
	public int getTotalPage() {

		if (TotalCount > 0) {
			return TotalCount % this.PageSize == 0 ? TotalCount / this.PageSize : TotalCount / this.PageSize + 1;
		} else {
			return 0;
		}

	}

	public void setTotalPage(int totalPage) {
		TotalPage = totalPage;
	}
}
